package com.qubuyer.business.mine.activity

import android.content.Intent
import android.os.Bundle
import androidx.recyclerview.widget.LinearLayoutManager
import com.blankj.utilcode.util.ConvertUtils
import com.qubuyer.R
import com.qubuyer.business.mine.adapter.RebateOrderDetailGoodListAdapter
import com.qubuyer.business.mine.viewmodel.WalletViewModel
import com.qubuyer.constant.NetConstant
import com.qubuyer.core.app.MVVMActivity
import com.qubuyer.core.ext.format
import com.qubuyer.core.ext.numberFormat
import com.qubuyer.customview.BrowserActivity
import com.qubuyer.customview.DecorationLLM
import com.qubuyer.databinding.LayoutActivityRebateOrderDetailBinding
import com.qubuyer.repository.entitys.RebateOrderEntity
import com.qubuyer.utils.DialogUtil
import java.util.*

/**
 * @author Susong
 * @date 创建时间2019/4/8
 * @description 折让订单详情
 */
class RebateOrderDetailActivity :
    MVVMActivity<LayoutActivityRebateOrderDetailBinding, WalletViewModel>() {

    override val layoutId: Int
        get() = R.layout.layout_activity_rebate_order_detail

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        viewModel.rebateOrderId = intent.getIntExtra("id", 0)
        dataBinding.titleBar.apply {
            setOnRightClick {
                startActivity(
                    Intent(mContext, BrowserActivity::class.java).putExtra(
                        BrowserActivity.KEY_URL,
                        NetConstant.BASE_URL + "/home/html/discountRule"
                    )
                )
            }
            setOnNaviClick {
                finish()
            }
        }
        viewModel.rebateOrder.observe(this, { entity ->
            dataBinding.data = entity
            dataBinding.tvCurrentReabteMoney.text = entity.detail_label_text
            dataBinding.horizontalProgressView.setProgressWithAnimation(entity.current_month_radio)
                .setCurrentProgressText("${entity.current_radio}")
            dataBinding.tvMaxRebateMoney.text = "最高折让: ${entity.expect_price.numberFormat(2)}元"
            dataBinding.tvOrderMoney.text = entity.total_price.toDouble().format(2)
            val list: MutableList<RebateOrderEntity> = ArrayList()
            list.add(entity)
            dataBinding.rvGoodList.apply {
                layoutManager = LinearLayoutManager(mContext)
                addItemDecoration(
                    DecorationLLM(
                        mContext,
                        DecorationLLM.VERTICAL_LIST,
                        R.drawable.shape_recyclerview_divider,
                        ConvertUtils.dp2px(15f)
                    )
                )
                adapter = RebateOrderDetailGoodListAdapter(list)
            }
        })
        viewModel.loadRebateOrderDetail()
    }

    fun restoreBalance() {
        DialogUtil.getConfirmDialog(
            mContext,
            "",
            "确定要提现到余额吗?",
            "确定",
            "取消",
            true,
            { _, _ ->
                viewModel.restoreBalance {
                    toast("提现成功")
                    finish()
                }
            }) { dialog, _ -> dialog.dismiss() }
            .show()
    }

    override val viewModel: WalletViewModel by injectVMByActivity()

    override fun onDataBind(dataBinding: LayoutActivityRebateOrderDetailBinding) {
        dataBinding.host = this
    }
}